﻿using DomainService.IService.Sys;
using Infrastructure;
using Infrastructure.IRepository.Sys;
using Infrastructure.Repository.Sys;
using Model.BusinessModel;
using Model.BusinessModel.OutPut.sys;
using Model.DomainModel.Sys;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DomainService.Service.Sys
{
    /// <summary>
    /// 登录日志服务接口
    /// </summary>
    public class SysLoginLogService : ISysLoginLogService
    {
        #region 构造实例化

        /// <summary>
        /// 登录日志底层仓储
        /// </summary>
        private readonly ISysLoginLogRepository _sysLoginLogRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="sysLoginLogRepository"></param>
        public SysLoginLogService(ISysLoginLogRepository sysLoginLogRepository)
        {
            _sysLoginLogRepository = sysLoginLogRepository;
        }

        #endregion

        #region 业务逻辑

        /// <summary>
        /// 新增登录日志
        /// </summary>
        /// <param name="sysLoginLog"></param>
        public void Insert(SysLoginLog sysLoginLog)
        {
            _sysLoginLogRepository.Insert(sysLoginLog, BaseSqlRepository.sysLoginLog_insertSql);
        }

        /// <summary>
        /// 获取登录日志
        /// </summary>
        /// <param name="pageResultModel">分页模型</param>
        /// <returns>返回指定数据</returns>
        public PageResultModel<LoginLogOutPut> GetLoginLogMsg(PageResultModel pageResultModel)
        {
            pageResultModel.tableField = "sc.CorporationName,sl.Ip,sl.Isp,sl.Country,sl.Prov,sl.City,sl.Area,sl.CreateTime,sl.Lat,sl.Lng,su.UserName";
            pageResultModel.tableName = @"[Sys_LoginLog] as sl
                            inner join Sys_Corporation as sc on sl.CorporationKey = sc.CorporationKey
                            inner join Sys_User as su on su.UserId = sl.UserId ";
            pageResultModel.orderByField = " sl.CreateTime ";
            pageResultModel.sortType = " desc ";
            var list = _sysLoginLogRepository.GetPageList<LoginLogOutPut>(pageResultModel);

            return list;
        }
        #endregion
    }
}
